/* libs/dllmain.cpp Written by, Sohail Qayum Malik<sohail@stupidcomputing.com> */

#include "CUnknown.h"

CUnknown::CUnknown() {

   m_RefCount = 1;
}
	
CUnknown::~CUnknown(void) {
}

STDMETHODIMP CUnknown::QueryInterface(REFIID riid, void** ppvObject) {        

   /* It is not as in "a young punk", it mearly is a "pointer to IUnknown interface" */
   IUnknown *punk = NULL;

   /* Every time for each requested interface, the same reference is returned. The requestor or caller has an instance, the caller wants to know if it is an instance of class which has implemented a certain interface(abstract class) */   
   if (riid == IID_IUnknown)            		 
	  punk = static_cast<IUnknown*>(this);        
			
	  // TODO: List other implemented interfaces in a similar manner.        
	  *ppvObject = punk; 
			
	  if (!punk)            
		 return E_NOINTERFACE;        
			   
	  punk->AddRef();        
	  return S_OK;    
}

STDMETHODIMP_(ULONG) CUnknown::AddRef() {        
	
   return ++m_RefCount;    
}				 
	 	 
STDMETHODIMP_(ULONG) CUnknown::Release() {        
	
   ULONG cRef = --m_RefCount;
	   
   if (cRef == 0)            
	  delete this;        
		  
   return cRef;    
}		 

__CUnknown_Header_DLL_EXPORT_IMPORT__ /* __declspec (dllexport) */ CUnknown *getCUnknown(void) {

   class CUnknown *ptr = new CUnknown();
   
   return ptr;   
}

/* When dll is loaded or unloaded */
BOOL APIENTRY DllMain(HINSTANCE hInst     /* Library instance handle. */ ,
                       DWORD reason        /* Reason this function is being called. */ ,
                       LPVOID reserved     /* Not used. */ ) {
					       
   switch (reason) {
   
      case DLL_PROCESS_ATTACH:
      break;

      case DLL_PROCESS_DETACH:
      break;

      case DLL_THREAD_ATTACH:
      break;

      case DLL_THREAD_DETACH:
      break;
   }

   /* Returns TRUE on success, FALSE on failure */
   return TRUE;
}
